package com.ruoyi.web.controller.workJob;

import com.google.common.collect.ImmutableMap;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.worker.domain.TermWorker;
import com.ruoyi.worker.domain.v2.*;
import com.ruoyi.worker.service.v2.RzAttendanceSumDailyRecordDetailV2Service;
import com.ruoyi.worker.service.v2.RzAttendanceSumDailyRecordV2Service;
import com.ruoyi.worker.service.v2.RzAttendanceSumMonthService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;

/**
 * @author: 陈宇寰
 * @date: 2021-12-25 21:34:44
 * @params:
 * @return:
 * @description: 考勤统计接口
 */
@RestController
@RequestMapping("/v2/workJobApi/monthlyAttendance")
public class RzAttendanceStatisticsV2Controller extends BaseController {

    @Resource
    private RzAttendanceSumDailyRecordV2Service sumDailyRecordV2Service;
    @Resource
    private RzAttendanceSumDailyRecordDetailV2Service detailV2Service;
    @Resource
    private RzAttendanceSumMonthService monthService;

    /**
     * @author: 陈宇寰
     * @date: 2021-12-29 15:53:28
     * @params:
     * @return:
     * @description: 构建日考勤结果
     */
    @GetMapping("/build")
    void buildSumDailyRecords(String date) {
        sumDailyRecordV2Service.buildSumDailyRecordWithDate(date);
    }

    /**
     * @author: 陈宇寰
     * @date: 2021-12-30 11:50:49
     * @params:
     * @return:
     * @description: 日考勤首页
     */
    @GetMapping("/statistics")
    AjaxResult statistics(String date, Integer groupId, Integer deptId) {
        ImmutableMap<String, Object> result = sumDailyRecordV2Service.statistics(date, groupId, deptId);
        return AjaxResult.success("ok", ObjectUtils.isEmpty(result) ? new HashMap<>() : result);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-03 20:59:39
     * @params:
     * @return:
     * @description: 日考勤明细
     */
    @GetMapping("/details")
    TableDataInfo detailStatistics(RzAttendanceSumDailyDetailQueryParameterVO vo) {
        startPage();
        List<RzAttendanceSumDailyDetailV2VO> vos = detailV2Service.detailStatistics2(vo);
        return getDataTable(vos);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-04 09:53:09
     * @params:
     * @return:
     * @description: 考勤调整
     */
    @PutMapping("/update")
    AjaxResult updateAttendanceDaily(@RequestBody List<RzAttendanceSumDailyDetailUpdateVO> datas) {
        datas.forEach(item -> {
            item.setUpdateBy(getUsername());
        });
        detailV2Service.updateAttendanceDaily(datas);
        return AjaxResult.success("ok");
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-04 09:52:57
     * @params:
     * @return:
     * @description: 变更记录
     */
    @GetMapping("/history")
    AjaxResult histories(RzAttendanceSumDailyDetailUpdateVO vo) {
        List<RzAttendanceSumDailyRecordDetailHistoryV2Domain> results = detailV2Service.histories(vo.getDate(), vo.getIdCards());
        return AjaxResult.success("查询成功", results);
    }


    /**
     * @author: 陈宇寰
     * @date: 2022-01-04 10:49:30
     * @params:
     * @return:
     * @description: 打卡明细
     */
    @GetMapping("/cards")
    AjaxResult cards(RzAttendanceSumDailyDetailUpdateVO vo) {
        List<Map<String, String>> results = detailV2Service.cardDetails(vo.getDate(), vo.getIdCards());
        return AjaxResult.success("查询成功", results);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-04 23:20:41
     * @params:
     * @return:
     * @description: 导出日考勤明细
     */
    @GetMapping("/export")
    public AjaxResult export(RzAttendanceSumDailyDetailQueryParameterVO vo) {
        List<RzAttendanceSumDailyDetailV2VO> list = detailV2Service.detailStatistics2(vo);
        ExcelUtil<RzAttendanceSumDailyDetailV2VO> util = new ExcelUtil<>(RzAttendanceSumDailyDetailV2VO.class);
        return util.exportExcel4Daily(list, "日考勤明细");
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-04 23:20:41
     * @params:
     * @return:
     * @description: 导出月考勤明细
     */
    @GetMapping("/month/export")
    public AjaxResult monthExport(RzAttendanceSumMonthDetailVO param) {
        List<RzAttendanceSumMonthDetailVO> vo = monthService.details(param);
        ExcelUtil<RzAttendanceSumMonthDetailVO> util = new ExcelUtil<>(RzAttendanceSumMonthDetailVO.class);
        return util.exportExcel(vo, "月考勤明细");
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-05 09:37:10
     * @params:
     * @return:
     * @description: 考勤确认
     */
    @PostMapping("/confirm")
    AjaxResult confirm(@RequestBody RzAttendanceSumDailyDetailQueryParameterVO vo) {
        sumDailyRecordV2Service.confirmRecord(vo.getDate(), String.valueOf(vo.getGroupId()), String.valueOf(vo.getDeptId()));
        return AjaxResult.success();
    }


    /**
     * @author: 陈宇寰
     * @date: 2022-01-05 15:26:58
     * @params:
     * @return:
     * @description: 月考勤首页数据
     */
    @GetMapping("/homeData")
    TableDataInfo monthIndexDatas(RzAttendanceSumMonthVO params) {
        startPage();
        List<RzAttendanceSumMonthVO> datas = monthService.indexDatas(params);
        return getDataTable(datas);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-05 16:04:27
     * @params:
     * @return:
     * @description: 异常考勤
     */
    @GetMapping("/invalid")
    AjaxResult invalidResult(RzAttendanceSumMonthVO params) {
        return AjaxResult.success("ok", monthService.invalidDatas(params));
    }


    /**
     * @author: 陈宇寰
     * @date: 2022-01-06 14:56:18
     * @params:
     * @return:
     * @description: 月考勤明细
     */
    @GetMapping("/month/detail")
    AjaxResult monthDetail(RzAttendanceSumMonthDetailVO param) {
        List<RzAttendanceSumMonthDetailVO> vo = monthService.details(param);
        return AjaxResult.success(vo);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-07 14:15:54
     * @params:
     * @return:
     * @description: em.....
     */
    @GetMapping("/month/details")
    AjaxResult monthDetails(RzAttendanceSumMonthDetailVO param) {
        List<RzAttendanceSumMonthDetailVO> vo = monthService.details(param);
        return AjaxResult.success("ok", vo);
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-06 22:16:26
     * @params:
     * @return:
     * @description: 查询可选项
     */
    @GetMapping("/options")
    AjaxResult options(RzAttendanceSumMonthDetailVO param) {
        return AjaxResult.success("ok", monthService.searchOptions(param));
    }

    /**
     * @author: 陈宇寰
     * @date: 2022-01-13 10:53:38
     * @params:
     * @return:
     * @description: 所有的签到时间
     */
    @GetMapping("/all/signTimes")
    public AjaxResult allSignTimes(String date) throws ParseException {
        List<DailySignTimesVO> vo = detailV2Service.signTimes(date);
        ExcelUtil<DailySignTimesVO> util = new ExcelUtil<>(DailySignTimesVO.class);
        return util.exportExcel(vo, "指定日期所有打卡时间");
    }
}
